import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";

export const constantRoutes = [
  //login
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true,
  },
  //404
  {
    path: "/404",
    component: () => import("@/views/404"),
    hidden: true,
  },
  // 首页
  {
    path: "/",
    component: Layout,
    redirect: "/home",
    children: [
      {
        path: "home",
        name: "Home",
        component: () => import("@/views/home/index.vue"),
        meta: {
          title: "首页",
          icon: "el-icon-house",
        },
      },
    ],
  },
  // 权限管理
  {
    path: "/acl",
    component: Layout,
    name: "Acl",
    meta: {
      title: "权限管理",
      icon: "el-icon-setting",
    },
    children: [
      {
        path: "user/list",
        name: "User",
        component: () => import("@/views/acl/user"),
        meta: {
          title: "用户管理",
        },
      },
      {
        path: "role/list",
        name: "Role",
        component: () => import("@/views/acl/role"),
        meta: {
          title: "角色管理",
        },
      },
      {
        path: "/menu/list",
        name: "Menu",
        component: () => import("@/views/acl/menu"),
        meta: {
          title: "菜单管理",
        },
      },
    ],
  },
  // 商品管理
  {
    path: "/product",
    component: Layout,
    name: "Product",
    meta: {
      title: "商品管理",
      icon: "el-icon-s-goods",
    },
    children: [
      {
        path: "category/list",
        name: "Category",
        component: () => import("@/views/product/category/index.vue"),
        meta: {
          title: "分类管理",
        },
      },
      {
        path: "trademark/list",
        name: "Trademark",
        component: () => import("@/views/product/trademark/index.vue"),
        meta: {
          title: "品牌管理",
        },
      },
      {
        path: "attr/list",
        name: "Attr",
        component: () => import("@/views/product/attr/index.vue"),
        meta: {
          title: "平台属性管理",
        },
      },
      {
        path: "spu/list",
        name: "Spu",
        component: () => import("@/views/product/spu/index.vue"),
        meta: {
          title: "SPU管理",
        },
      },
      {
        path: "sku/list",
        name: "Sku",
        component: () => import("@/views/product/sku/index.vue"),
        meta: {
          title: "SKU管理",
        },
      },
    ],
  },

  // 匹配任意路由 前面的匹配不到时匹配  必须最后注册
  { path: "*", redirect: "/404", hidden: true },
];

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
  });

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
